import 'dart:convert' show json;

import 'package:flutter/cupertino.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

import 'base_chart_formatter.dart';
import 'base_model_asT.dart';

class BaseChartNestedPiesModel {
  /// https://echarts.apache.org/zh/option.html#tooltip.formatter
  BaseChartFormatter tooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// 图表样式
  BaseChartStyle style;

  /// 图表数据
  List<BaseChartNestedPiesData> data;

  /// 内部饼图数据
  List<BaseChartNestedPiesData> innerData;

  BaseChartNestedPiesModel(
      {this.tooltipFormatter,
      this.labelFormatter,
      this.style,
      @required this.data,
      this.innerData});

  factory BaseChartNestedPiesModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<BaseChartNestedPiesData> data =
        jsonRes['data'] is List ? <BaseChartNestedPiesData>[] : null;
    final List<BaseChartNestedPiesData> innerData =
        jsonRes['innerData'] is List ? <BaseChartNestedPiesData>[] : null;

    if (data != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          data.add(BaseChartNestedPiesData.fromJson(
              asT<Map<String, dynamic>>(item)));
        }
      }
    }

    if (innerData != null) {
      for (final dynamic item in jsonRes['innerData']) {
        if (item != null) {
          innerData.add(BaseChartNestedPiesData.fromJson(
              asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return BaseChartNestedPiesModel(
        tooltipFormatter: asT<BaseChartFormatter>(jsonRes['tooltipFormatter']),
        labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
        style: asT<BaseChartStyle>(jsonRes['style']),
        data: data,
        innerData: innerData);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'tooltipFormatter': tooltipFormatter,
        'labelFormatter': labelFormatter,
        'data': data,
        'style': style,
        'innerData': innerData
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartNestedPiesData {
  BaseChartNestedPiesData({
    String name,
    this.value,
    this.itemStyle
  }):this.name=name.replaceFirst('-', '-\n').replaceAll(' ', '\n');

  factory BaseChartNestedPiesData.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartNestedPiesData(
              name: asT<String>(jsonRes['name']),
              value: asT<double>(jsonRes['value']),
              itemStyle: asT<Map<String, dynamic>>(jsonRes['itemStyle']),
            );

  /// 数据名称
  String name;

  /// 数据值
  double value;

  /// https://echarts.apache.org/zh/option.html#series-bar.itemStyle
  Map<String, dynamic> itemStyle;

  Map<String, dynamic> toJson() {
    var json = <String, dynamic>{
      'name': name,
      'value': value,
      'itemStyle': itemStyle
    };
    json.removeWhere((key, value) => value == null);
    return json;
  }

  @override
  String toString() {
    return json.encode(this);
  }
}
